/*
 * @(#) ExportExcel.java 2015-5-20
 *
 * Copyright (c) 2014, SIMPO Technology. All Rights Reserved.
 * SIMPO Technology. CONFIDENTIAL
 */
package com.sjq.common.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import com.sjq.yqgl.entity.Yqbs;


/**
 * 导出模板
 *
 * @author dingke
 * @version 1.0
 * @since 2015-5-20
 */
public class ExportExcelUtil<T> {
	


	/**
	 * 这是一个通用的方法，利用了JAVA的反射机制，可以将放置在JAVA集合中并且符号一定条件的数据以EXCEL 的形式输出到指定IO设备上
	 * 
	 * @param title 表格标题名
	 * @param headers 表格属性列名数组
	 * @param dataset 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。此方法支持的 javabean属性的数据类型有基本数据类型及String,Date,byte[](图片数据)
	 * @param out 与输出设备关联的流对象，可以将EXCEL文档导出到本地文件或者网络中
	 * @param pattern 如果有时间数据，设定输出格式。默认为"yyy-MM-dd"
	 */
	@SuppressWarnings("unchecked")
	public void exportExcel(String title,String titler, String[] headers, ArrayList<Yqbs> dataset,
			OutputStream out) {
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workbook.createSheet(title);
		// 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth(15);
      
        // //标题样式
		HSSFCellStyle style_title = workbook.createCellStyle();
		// 设置这些样式
		//style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		//style_title.setAlignment(HorizontalAlignment.CENTER);
		style_title.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		/*style_title.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style_title.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style_title.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style_title.setBorderTop(HSSFCellStyle.BORDER_THIN);*/
		style_title.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		//style_title.set
		//标题字体
		HSSFFont font_title = workbook.createFont();
		font_title.setColor(HSSFColor.BLACK.index);
		font_title.setFontHeightInPoints((short)20);
		font_title.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font_title.setFontName("宋体");
		style_title.setFont(font_title);
		 // //主标题
		HSSFCellStyle style_title2 = workbook.createCellStyle();
		// 设置这些样式
		//style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		//style_title.setAlignment(HorizontalAlignment.CENTER);
		/*style_title2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style_title2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style_title2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style_title2.setBorderTop(HSSFCellStyle.BORDER_THIN);*/
		style_title2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style_title2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		style_title2.setWrapText(true);
		HSSFFont font_title2 = workbook.createFont();
		font_title2.setColor(HSSFColor.BLACK.index);
		font_title2.setFontHeightInPoints((short)12);
		font_title2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font_title2.setFontName("宋体");
		style_title2.setFont(font_title2);
		
		HSSFCellStyle style_title3 = workbook.createCellStyle();
		// 设置这些样式
		//style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		//style_title.setAlignment(HorizontalAlignment.CENTER);
		/*style_title3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style_title3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style_title3.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style_title3.setBorderTop(HSSFCellStyle.BORDER_THIN);*/
		style_title3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFFont font_title3 = workbook.createFont();
		font_title3.setColor(HSSFColor.BLACK.index);
		font_title3.setFontHeightInPoints((short)12);
	//	font_title3.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font_title3.setFontName("宋体");
		style_title3.setFont(font_title3);
		
	
		
		
		
		// 生成并设置另一个样式
		HSSFCellStyle style2 = workbook.createCellStyle();
		/*style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);*/
	//	style2.set
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 生成另一个字体
		HSSFFont font2 = workbook.createFont();
		font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		// 把字体应用到当前的样式
		style2.setFont(font2);
		 CellRangeAddress region1=new CellRangeAddress(0,0,0,14);
		 sheet.addMergedRegion(region1);
		//标题
		 HSSFRow row0 = sheet.createRow(0);
		 row0.setHeight((short)650);
		 HSSFCell cell0 = row0.createCell(0);
		 cell0.setCellStyle(style_title);
		 HSSFRichTextString text1 = new HSSFRichTextString(titler);
		 cell0.setCellValue(text1);
		 
		//签发
		HSSFRow row1 = sheet.createRow(1);
		 HSSFCell cell_0 = row1.createCell(0);
		 cell_0.setCellStyle(style_title2);
		 cell_0.setCellValue( new HSSFRichTextString("填报单位"));
		
		 HSSFCell cell_2 = row1.createCell(2);
		 cell_2.setCellStyle(style_title2);
		 cell_2.setCellValue( new HSSFRichTextString("填报时间"));
		 HSSFCell cell3 = row1.createCell(4);
		 cell3.setCellStyle(style_title2);
		 cell3.setCellValue( new HSSFRichTextString("填报人"));
		 HSSFCell cell5 = row1.createCell(6);
		 cell5.setCellStyle(style_title2);
		 cell5.setCellValue( new HSSFRichTextString("审核人"));
		 row1.setHeight((short)350);
		 
		// 产生表格标题行
		HSSFRow row2 = sheet.createRow(2);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row2.createCell(i);
			cell.setCellStyle(style_title2);
			HSSFRichTextString text = new HSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}
        row2.setHeight((short)1350);
        //生成疫情报送下发数据
        for(int j=0;j<dataset.size();j++){
        	row2 =sheet.createRow(j+3);
        	Yqbs yqbs=	dataset.get(j);
        	
        	HSSFCell cell = row2.createCell(0);
        	cell.setCellStyle(style_title3);
        	HSSFRichTextString xh = new HSSFRichTextString(""+(j+1));//序号
			cell.setCellValue(xh);
			
			
			HSSFCell cell_xm = row2.createCell(1);
			cell_xm.setCellStyle(style_title3);
        	HSSFRichTextString xm = new HSSFRichTextString(yqbs.getXm());
        	cell_xm.setCellValue(xm);
			
			HSSFCell cell_sjhm = row2.createCell(2);
			cell_sjhm.setCellStyle(style_title3);
        	HSSFRichTextString sjhm = new HSSFRichTextString(yqbs.getSjhm());
        	cell_sjhm.setCellValue(sjhm);
			
			HSSFCell cell_sfhm = row2.createCell(3);
			cell_sfhm.setCellStyle(style_title3);
        	HSSFRichTextString sfhm = new HSSFRichTextString(yqbs.getSfhm());
        	cell_sfhm.setCellValue(sfhm);
        }
       	try {
			workbook.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

/*	private static void setSizeColumn(HSSFCell sheet, int size) {
        for (int columnNum = 0; columnNum < size; columnNum++) {
            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
            for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
                XSSFRow currentRow;
                //当前行未被使用过
                if (sheet.getRow(rowNum) == null) {
                    currentRow = sheet.createRow(rowNum);
                } else {
                    currentRow = sheet.getRow(rowNum);
                }
                if (currentRow.getCell(columnNum) != null) {
                    XSSFCell currentCell = currentRow.getCell(columnNum);
                    if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                        int length = currentCell.getStringCellValue().getBytes().length;
                        if (columnWidth < length) {
                            columnWidth = length;
                        }
                    }
                }
            }
            sheet.setColumnWidth(columnNum, columnWidth * 256);
        }
    }*/
	
	
    public static void main(String[] args) throws FileNotFoundException {
    	String sheetTitle="";
    	ExportExcelUtil<Yqbs> ex=new ExportExcelUtil();
    	ArrayList<Yqbs> dataset=new ArrayList<Yqbs>(); 
    	
    	 String[] headers={"序号","姓名","手机号码","身份证号","户籍地址","现住地址","核查方式（电话/见面）","回宿时间","当前位置（是否在宿）","有无身体不适（发热/咳嗽/其它呼吸道症状/无）","防护措施（居家观察/医学隔离）","社区包保人员","联系电话","社区民警","联系电话"};
    	 Yqbs exp=new Yqbs();
    	 exp.setXh("1");
    	 exp.setXm("丁可");
    	 exp.setSjhm("3213211991206016257");
    	 exp.setSfhm("4");
        dataset.add(exp);
        System.out.println("-----");
    	OutputStream out= new FileOutputStream("d:/a.xls");
    	//ex.exportExcel("ceshi",headers, dataset,out);
	}

}
